package lemna.top.chunyu.batch.config;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import lombok.extern.log4j.Log4j;

@Log4j
@Component
public class RunScheduler {

	@Autowired
	private JobLauncher jobLauncher;


	@Bean
	public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
		SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
		simpleJobLauncher.setJobRepository(jobRepository);
		return simpleJobLauncher;
	}
	@Autowired
	private Job importUserJob;
	
	@Scheduled(fixedRate = 925000)
	//@Scheduled(cron = "1 53/3 17 * * ?")
	public void run() {

		log.info("------------------------------start----------------------------------");
		try {
			
			Date today = new Date();
			today.setDate(26);
			today.setMonth(6);
//			JobParameters param = new JobParametersBuilder().addDate("date", new Date()).toJobParameters();
			JobParameters param = new JobParametersBuilder().addDate("date",today ).toJobParameters();


			JobExecution execution = jobLauncher.run(importUserJob, param);
			System.out.println("Exit Status : " + execution.getStatus());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
